<?php
    class Admincp_Model_Room extends Zend_Db_Table_Abstract
    {
        protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem($data)
        {
            $sql = $this->_db->select()
                            ->from('room AS r')
                            ->join('room_language AS rl', 'rl.room_id = r.id', array('name'))
                            ->where('r.hotel_id = ?', $data['hotel_id'])
                            ->where('r.type = ?', $data['type'])
                            ->where('rl.language_id = ?', 5)
                            ->order('r.id DESC');
            return $sql;
        }
        
        public function saveItem($data, $option = null)
        {
            if($option == 'language') {
                $this->_db->insert('room_language',$data);
            }
            else {
                $this->_db->insert('room',$data);
                return $this->_db->lastInsertId();
            }
        }
        
        public function updateItem($data, $option = null)
        {
            if($option == 'language') {
                $where = 'room_id = '.$data['room_id'].' AND language_id = '.$data['language_id'];
                $this->_db->update('room_language', $data, $where);
            }
            else {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update('room', $data, $where);
            }
        }
        
        public function deleteItem($id, $type = null)
        {
            if($type != null) {
                $sql = $this->_db->select()
                                ->from('room', array('id'))
                                ->where('type = ?', $type)
                                ->where('hotel_id = ?', $id);
                $result = $this->_db->fetchAll($sql);
                foreach($result as $value) {
                    $where = $this->getAdapter()->quoteInto('id = ?', $value);
                    $this->_db->delete('room', $where);
                    $where = $this->getAdapter()->quoteInto('room_id = ?', $value);
                    $this->_db->delete('room_language', $where);
                }
            }
            else {
                $where = $this->getAdapter()->quoteInto('id = ?', $id);
                $this->_db->delete('room', $where);
                $where = $this->getAdapter()->quoteInto('room_id = ?', $id);
                $this->_db->delete('room_language', $where);
            }
        }
        
        public function deleteMultiItem($listId)
        {
            foreach($listId as $id) {
                $this->deleteItem($id);
            }
        }
        
        public function getItem($id, $option = null)
        {
            $sql = $this->_db->select();
            if($option == 'language') {
                $sql->from('room_language AS rl')
                    ->join('language AS l', 'rl.language_id = l.id', array('locale'))
                    ->where('rl.room_id = ?', $id);
                $result = $this->_db->fetchAll($sql);
                
            }
            else {
                $sql->from('room')
                    ->where('id = ?', $id);
                $result = $this->_db->fetchRow($sql);
            }
            return $result;
        }
    }
